package org.mspring.mlog.schedule.job;

import java.util.List;

import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.mspring.mlog.api.spider.Spider;
import org.mspring.mlog.api.spider.crawler.DefaultCrawler;
import org.mspring.mlog.api.spider.entity.PublishRule;
import org.mspring.mlog.api.spider.entity.SpiderPost;
import org.mspring.mlog.api.spider.handler.impl.ContentImageHandler;
import org.mspring.mlog.api.spider.service.PublishRuleService;
import org.mspring.mlog.api.spider.service.SpiderPostService;
import org.mspring.mlog.api.spider.utils.DocumentUtils;
import org.mspring.mlog.core.ServiceFactory;
import org.mspring.platform.utils.StringUtils;
import org.quartz.JobExecutionContext;

/**
 * 
 * @author Gao Youbo
 * @since 2013年7月23日
 */
public class SpiderJob extends BaseJob {
    private static final Logger log = Logger.getLogger(SpiderJob.class);

    private PublishRuleService publishRuleService = ServiceFactory.getBean(PublishRuleService.class);
    private SpiderPostService spiderPostService = ServiceFactory.getBean(SpiderPostService.class);

    @Override
    protected void nativeExecuteInternal(JobExecutionContext context) {
        // TODO Auto-generated method stub
        String key = context.getJobDetail().getKey().getName();
        if (StringUtils.isBlank(key)) {
            return;
        }
        PublishRule publishRule = publishRuleService.getByJob(new Long(key));

        if (publishRule.getRule() == null) {
            log.warn("无法找到采集规则");
            return;
        }

        Spider spider = new Spider(new DefaultCrawler());
        spider.addContentHandler("contentImage", new ContentImageHandler());

        log.debug("开始..." + publishRule.getName());
        log.debug("开始获取列表：" + publishRule.getRule().getUrl());

        Document listPageDoc = DocumentUtils.getDocument(publishRule.getRule().getUrl());
        List<String> urls = spider.getUrls(listPageDoc, publishRule.getRule());

        Document contentDoc = null;
        SpiderPost spiderPost = null;
        for (String url : urls) {
            if (StringUtils.isNotBlank(url)) {
                log.debug("分析网址：" + url);

                if (spiderPostService.exists(url)) {
                    continue;
                }

                Element title = null;
                Element content = null;

                try {
                    contentDoc = DocumentUtils.getDocument(url);
                    title = spider.getTitle(contentDoc, publishRule.getRule());
                    content = spider.getContent(contentDoc, publishRule.getRule());
                } catch (Exception e) {
                    // TODO: handle exception
                    log.error("抓取文章失败", e);
                    continue;
                }

                if (title == null || content == null) {
                    log.error("抓取文章失败");
                    continue;
                }

                log.debug("文章：" + title);
                try {
                    spiderPost = new SpiderPost();
                    spiderPost.setSpiderRule(publishRule.getRule().getId());
                    spiderPost.setPublishRule(publishRule.getId());
                    spiderPost.setTitle(title.text());
                    spiderPost.setContent(content.html());
                    spiderPost.setSourceUrl(url);
                    spiderPost = spiderPostService.createSpiderPost(spiderPost);

                    // 判断是否自动发布文章
                    if (publishRule.getAutoPublish()) {
                        spiderPostService.publishPost(publishRule.getPublishAuthor(), publishRule.getPublishCatalog(), spiderPost);
                    }
                    log.debug("导入成功");
                } catch (Exception e) {
                    // TODO: handle exception
                    log.error("导入失败", e);
                    continue;
                }

                if (publishRule.getTimeInterval() != null && publishRule.getTimeInterval() > 0) {
                    try {
                        Thread.sleep(publishRule.getTimeInterval()); // 暂停，防止被列入黑名单
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
        log.debug("全部执行完成。");
    }

}
